import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'dart:io';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class AccountSettingsController extends GetxController {
  String avatarFilePath = "";

  Future<void> chooseImage() async {
    final List<AssetEntity>? result = await AssetPicker.pickAssets(
      Get.context!,
      pickerConfig: AssetPickerConfig(
        maxAssets: 1,
        requestType: RequestType.image,
        specialPickerType: SpecialPickerType.noPreview,
        textDelegate: const AssetPickerTextDelegate(),
      ),
    );
    if (result != null) {
      final selectedAsset = result.firstOrNull;
      var image = await selectedAsset?.file;
      if (image != null) {
        avatarFilePath = await _cropImage(image) ?? "";
      }
      update();
    }
  }

  Future<String?> _cropImage(File image) async {
    CroppedFile? croppedFile = await ImageCropper().cropImage(sourcePath: image.path, aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0), maxWidth: 400, maxHeight: 400, uiSettings: [
      AndroidUiSettings(toolbarTitle: '图片裁剪', cropStyle: CropStyle.circle, toolbarColor: Colors.white, toolbarWidgetColor: Colors.black, initAspectRatio: CropAspectRatioPreset.original, lockAspectRatio: true),
      IOSUiSettings(
        title: '图片裁剪',
        minimumAspectRatio: 1.0,
      )
    ]);
    return croppedFile?.path;
  }
}
